package com.bjqh.edu.UI.Fragment;


import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.ListFragment;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AlertDialog;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.baijiahulian.live.ui.LiveSDKWithUI;
import com.bjqh.edu.Base.AppConstants;
import com.bjqh.edu.Bean.Login;
import com.bjqh.edu.Bean.Room;
import com.bjqh.edu.R;
import com.bjqh.edu.UI.Activity.Login.LoginActivity;
import com.bjqh.edu.UI.Utils.SharedPrefsUtil;
import com.bjqh.edu.adapter.PendingAdapter2;
import com.google.gson.Gson;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.cache.CacheMode;
import com.lzy.okgo.callback.StringCallback;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;
import okhttp3.Call;
import okhttp3.Response;


public class PendingCoursesFragment extends ListFragment implements SwipeRefreshLayout.OnRefreshListener {
    @BindView(android.R.id.list)
    ListView list;
    @BindView(android.R.id.empty)
    LinearLayout empty;
    @BindView(R.id.swipeLayout)
    SwipeRefreshLayout swipeLayout;
    @BindView(R.id.tip)
    TextView tip;


    private PendingAdapter2 adapter;
    private List<Room.DataBean> dataBeen;
    Unbinder unbinder;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_pending_courses, null, false);
        ButterKnife.bind(this, view);
        unbinder = ButterKnife.bind(this, view);
        //  View emptyview = inflater.inflate(R.layout.empty_view, null, false);
        list.setEmptyView(empty);
        swipeLayout.setColorSchemeResources(R.color.grean);
        swipeLayout.setOnRefreshListener(this);
        getValue();
        tip.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                AlertDialog.Builder normalDialog =
                        new AlertDialog.Builder(getActivity()).
                                setTitle("小主，您要给4006326977打电话吗？？")
                                .setPositiveButton("确定",
                                        new DialogInterface.OnClickListener() {
                                            @Override
                                            public void onClick(DialogInterface dialog, int which) {
                                                Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + "4006326977"));
                                                startActivity(intent);

                                            }
                                        });
                normalDialog.setNegativeButton("取消",
                        new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                dialog.dismiss();
                            }
                        }).create();
                // 显示
                normalDialog.show();
            }
        });
        return view;
    }

    @Override
    public void onListItemClick(ListView l, View v, int position, long id) {
        super.onListItemClick(l, v, position, id);
        if (position==0)
        {
            Log.e("进入房间", "进入房间");
            Gson gson = new Gson();
        /*SharedPreferences preference = null;
        preference = getActivity().getSharedPreferences("userInfo", 0);*/
            String user = SharedPrefsUtil.getValue(getActivity(),"user","");

            if (!TextUtils.isEmpty(user)) {
                Login login = gson.fromJson(user, Login.class);
                Login.UserBean userBean = login.getUser();
                if (userBean != null) {
                    LiveSDKWithUI.enterRoom(getActivity(), dataBeen.get(position).getInvitecode(), login.getUser().getName(), AppConstants.IMG_URL + login.getUser().getHeader(), new LiveSDKWithUI.LiveSDKEnterRoomListener() {
                        @Override
                        public void onError(String s) {
                            Toast.makeText(getActivity(), "报告小主，进入房间失败啦~~", Toast.LENGTH_SHORT).show();
                        }
                    });

     /*   Intent intent = new Intent(getActivity(), LiveRoomActivity.class);
        BjyUser bj=new BjyUser();
        bj.setAvatar(AppConstants.IMG_URL+login.getUser().getHeader());
        bj.setName(login.getUser().getName());
        bj.setUserId(userBean.getId()+"");

        intent.putExtra("code", dataBeen.get(position).getInvitecode());
        intent.putExtra("user",bj);
        getActivity().startActivity(intent);*/

                }
                LiveSDKWithUI.setRoomExitListener(new LiveSDKWithUI.LPRoomExitListener() {
                    @Override
                    public void onRoomExit(Context context, LiveSDKWithUI.LPRoomExitCallback lpRoomExitCallback) {
                        lpRoomExitCallback.exit();
                    }
                });


            }


        }


    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();

    }

    public void getValue() {
        OkGo.init(getActivity().getApplication());
        OkGo.post(AppConstants.BASE_URL + "/rtu/myRoom.shtml")
                .tag(this)
                .cacheKey("cacheKey")
                .cacheMode(CacheMode.DEFAULT)
                .params("status", 4)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(String s, Call call, Response response) {
                        swipeLayout.setRefreshing(false);
                        Log.i("待上课程", s);
                        Gson gson = new Gson();
                        Room room = gson.fromJson(s, Room.class);
                        if (room.getMsg() != null) {
                            if (room.getMsg().equals("timeout")) {
                                AppConstants.gotoLogin(getActivity());
                            } else if (room.getMsg().equals("remote login")) {
                                AppConstants.remologin(getActivity());
                            }
                        } else {
                            dataBeen = room.getData();
                            if (dataBeen != null) {
                                adapter = new PendingAdapter2(getContext(), dataBeen);
                                list.setAdapter(adapter);
                            }

                        }


                    }

                    @Override
                    public void onError(Call call, Response response, Exception e) {
                        super.onError(call, response, e);
                        Toast.makeText(getActivity(), "报告小主，服务器被外星人偷走啦~~", Toast.LENGTH_SHORT).show();
                    }
                });
    }

    @Override
    public void onRefresh() {
        getValue();
    }

}

